<?php

/**
 * Description of myFunctionsclass
 *
 * @author miguel
 */
class myFunctionsTables {

    public static function setFilterTable($table_column, $column_name, $filter, $user) {

            $filter = implode('-', $filter);


            $q = Doctrine_Query::create()
                    ->from('UserTableFilter utf')
                    ->where('utf.table_column = ?', array($table_column))
                    ->addWhere('utf.column_name = ?', array($column_name))
                    ->addWhere('utf.id_users = ?', array($user));

            $userFilter = $q->fetchArray();
            $userFilterCount = count($q->fetchArray());


            if ($userFilterCount) {

                $_data = Doctrine_Query::create()
                        ->update('UserTableFilter utf')
                        ->set('utf.column_ids', '?', ((is_null($filter)?'':$filter)))
                        ->where('utf.id_users = ?', array($user))
                        ->addWhere('utf.table_column = ?', array($table_column))
                        ->addWhere('utf.column_name = ?', array($column_name))
                        ->execute();
            } else {

                try {

                    $tabla = Doctrine::getTable('UserTableFilter');
                    $tabla = new UserTableFilter();

                    $tabla->id_users = $user;
                    $tabla->table_column = $table_column;
                    $tabla->column_name = $column_name;
                    $tabla->column_ids = ((is_null($filter)?'':$filter));

                    $tabla->save();
                } catch (Exception $e) {
                    syslog(LOG_ERR, "Error: " . $e->getMessage());
                }
            }
    }

    public static function getFilterTable($table_column, $column_name, $tableId, $type, $user) {

        $q = Doctrine_Query::create()
                ->from('UserTableFilter utf')
                ->where('utf.id_users = ?', array($user))
                ->addWhere('utf.column_name = ?', array($column_name))
                ->addWhere('utf.table_column = ?', array($table_column));

        $columnFilter = $q->fetchArray();

        $tableIds = explode('-', $columnFilter[0]['column_ids']);


        $q = Doctrine_Query::create()
                ->from("$column_name as t")
                ->innerJoin("t.Stores s on s.$tableId = t.$tableId")
                ->whereIn($tableId, $tableIds)
                ->groupBy("t.$tableId");
        

        $dataFilter = $q->fetchArray();

        foreach ($dataFilter as $value) {
            $dataArray[] = array($value[$tableId], "$type='$type'", $value['name']);
        }

       $q = Doctrine_Query::create()
                ->from("$column_name as t")
                ->innerJoin("t.Stores s on s.$tableId = t.$tableId")
                ->whereNotIn($tableId, $tableIds)
                ->groupBy("t.$tableId");

        $dataNoFilter = $q->fetchArray();

        foreach ($dataNoFilter as $value) {
            $dataArray[] = array($value[$tableId], "", $value['name']);
        }

        
        $dataArray = myFunctions::orderMultiDimensionalArray($dataArray, 2);
        return $dataArray;
    }

    
    public static function setOrderTable($table_column, $order, $visible, $user, $table_user) {
        
        
        $q = Doctrine_Query::create()
                ->from('UserTable ut')
                ->leftJoin('ut.UserTableOrder uto')
                ->where('ut.table_user = ?', array($table_column))
                ->addWhere('uto.id_users = ?', array($user));

        $userOrderCount = count($q->fetchArray());
        
        if($userOrderCount){
            
            $i = 1;
            foreach ($order as $value) {
               $_data = Doctrine_Query::create()
                ->update('UserTableOrder uto')
                ->set('uto.orden', '?', $i);
               
                if($visible[$value]){
                    $_data->set('uto.visible', '?', $visible[$value]);
                }else{
                    $_data->set('uto.visible', '?', 0);
                }
                
                $_data->where('uto.id_users = ?', array($user))
                ->addWhere('uto.id_tablecolumn = ?', array($value))
                ->execute();
             $i++;
            }
             
        }else{
            
            $i = 1;
            foreach ($order as $value) {
             
                try {

                    $tabla = Doctrine::getTable('UserTableOrder');
                    $tabla = new UserTableOrder();

                    $tabla->id_tablecolumn  = $value;
                    $tabla->id_users        = $user;
                    $tabla->orden           = $i;
                    $tabla->visible         = $visible[$value];
                    $tabla->table_user      = $table_user;

                    $tabla->save();
                    $i++;
                } catch (Exception $e) {
                    syslog(LOG_ERR, "Error: " . $e->getMessage());
                }
            }

        }
        
        
    }
    
    
    
    public static function getFilterTableIds($table_column, $column_name, $user) {

        $q = Doctrine_Query::create()
                ->from('UserTableFilter utf')
                ->where('utf.id_users = ?', array($user))
                ->addWhere('utf.column_name = ?', array($column_name))
                ->addWhere('utf.table_column = ?', array($table_column));

        $columnFilter = $q->fetchArray();
        
        $tableIds = explode('-', $columnFilter[0]['column_ids']);

        return $tableIds;
    }
    
}

?>
